<?php
/**
 * 學生異動資料
 * @author cik
 *
 */
class Default_Model_Upgrade_StudMove extends Default_Model_Upgrade_Core
{
    protected $_table = 'stud_move';

    protected $_targetTable = array('student_transfer');

    function upgrade (){
        if ($this->isUpgrade())
        return 'isUpgrde';
        else {
            $hasData = 0 ;
            // 先判斷是否有資料
            $query = "SELECT COUNT(*)  AS cc FROM student_transfer";
            $res = $this->_newdbPdo->query($query)->fetch();
            $hasData = $res['cc'];


            $query = "ALTER TABLE `student_transfer`  DROP  FOREIGN KEY `student_transfer_ibfk_1`  ";
            $this->_newdbPdo->exec($query);;

            $query = "ALTER TABLE `student_transfer`  ENGINE = MYISAM";
            $this->_newdbPdo->exec($query);

            $query = "SELECT * FROM stud_move " ;
            $res = $this->_sfs3Pdo->query($query)->fetchAll(PDO::FETCH_ASSOC);
            foreach($res as $row) {
                $arr = array();
                $year = substr($row['move_year_seme'],0,-1);
                $semester = substr($row['move_year_seme'],-1);
                $arr['year']  = $year;
                $arr['semester'] =$semester;
                $arr['student_sn'] = $row['student_sn'];
                if ($hasData) {
                    $find = $this->_newdbPdo->query("SELECT student_sn FROM student_transfer
				  WHERE  student_sn ='{$arr['student_sn']}'
				  AND year ='{$arr['year']}'
				  AND semester ='{$arr['semester']}'
				  ");
                    if ($find->rowCount() > 0)
                    continue;
                }
                $arr['county'] =  $this->iconv($row['city']);
                $arr['school_name'] = $this->iconv($row['school']);
                $arr['school_id'] = $this->iconv($row['school_id']);
                $arr['date'] = $row['move_date'];
                $arr['approve_organ'] = $this->iconv($row['move_c_unit']);
                $arr['causation'] = $this->iconv($row['reason']);
                $arr['causation_kind'] = $row['move_kind'];
                $arr['approve_date'] = $row['move_c_date'];
                $arr['approve_word'] = $this->iconv($row['move_c_word']);
                $arr['approve_number'] = $this->iconv($row['move_c_num']);
                $arr['teacher_sn'] = Teacher_Model_Teacher::getSnById($row['update_id']);
                $arr['update_time'] = $row['update_time'];
                $arr['school_move_num'] = $row['school_move_num'] ;
                $this->fromArray('student_transfer',$arr);
            }

            $query = "ALTER TABLE `student_transfer`  ENGINE = INNODB";
            $this->_newdbPdo->exec($query);
            $query = "ALTER TABLE `student_transfer`
  ADD CONSTRAINT `student_transfer_ibfk_1` FOREIGN KEY (`student_sn`) REFERENCES `student` (`sn`) ON DELETE CASCADE ON UPDATE CASCADE;
            ";
            $this->_newdbPdo->exec($query);;

            $log['memo'] = 'stud_move  升級';
            $log['upgrade_time']	= date("Y-m-d h:i:s");

            $this->finishUpgrade($log);

        }
    }
}
